//
//  JRHttpRequestManagerTest.swift
//  JRHttpRequestManager
//
//  Created by 京睿 on 2017/4/27.
//  Copyright © 2017年 JingRuiWangKe. All rights reserved.
//

import XCTest
@testable import JRHttpRequestManager

class JRHttpRequestManagerTest: XCTestCase {
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
        kConfig.baseUrl = "https://test.jingruiwangke.com/jrClientApp"
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }
    
    func testPerformanceExample() {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }
    
    func testPOSTRequest() {
        let exp = expectation(description: "post")
        JRHttpRequestManager
            .init(POST: "common", params: [
                "action": "index",
                "areaId": "10010101"
                ])
            .successful {
                XCTAssertNotNil($0)
                exp.fulfill()
            }
            .failed {
                XCTFail($0 ?? "NULL f")
        }
        waitForExpectations(timeout: 10, handler: nil)
    }
    
    func testGETRequest() {
        let exp = expectation(description: "get")
        JRHttpRequestManager
            .init(GET: "common?action=index&areaId=10010101")
            .successful {
                XCTAssertNotNil($0)
                exp.fulfill()
            }
            .failed {
                XCTFail($0 ?? "NULL f")
        }
        waitForExpectations(timeout: 10, handler: nil)
    }
    
    func testPOSTFailRequest() {
        let exp = expectation(description: "post")
        JRHttpRequestManager
            .init(POST: "common404", params: [
                "action": "index",
                "areaId": "10010101"
                ])
            .successful { _ in
                XCTFail("NULL f")
                exp.fulfill()
            }
            .failed {
                XCTAssertNotNil($0)
                exp.fulfill()
        }
        waitForExpectations(timeout: 10, handler: nil)
    }
    
    func testGETFailRequest() {
        let exp = expectation(description: "get")
        JRHttpRequestManager
            
            .init(GET: "common500?action=index&areaId=10010101")
            
            .successful { _ in
                XCTFail("NULL f")
                exp.fulfill()
            }
            
            .failed {
                XCTAssertNotNil($0)
                exp.fulfill()
        }
        waitForExpectations(timeout: 10, handler: nil)
    }
    
    func testUnexpectedCode() {
        let exp = expectation(description: "post")
        JRHttpRequestManager
            .init(POST: "common", params: [
                "action": "indexnotexist",
                "areaId": "10010101"
                ])
            .successful { _ in
                XCTFail("NULL f")
                exp.fulfill()
            }
            .failed {
                XCTAssertNotNil($0)
                exp.fulfill()
        }
        waitForExpectations(timeout: 10, handler: nil)
    }
}
